package com.prp.gwt.client.user.php;

import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.Window;
import com.prp.gwt.client.ClientConstants;
import com.prp.gwt.client.Host;
import com.prp.gwt.client.user.friend.FriendListener;
import com.prp.gwt.client.widgets.UserBarPanel;
import com.prp.gwt.client.widgets.user.php.FriendsPreviewPanel;
import com.prp.gwt.client.widgets.user.php.MsgBoardPanel;
import com.prp.gwt.client.widgets.user.php.RecentRecommendPanel;
import com.prp.gwt.client.widgets.user.php.UserInfoPanel;
import com.prp.gwt.client.widgets.user.php.UserStateListPanel;

public class PHPHistoryHandler implements ValueChangeHandler<String> {

	public static final String PHP = "php";
	public static final String PREFIX_USERHOME = "userhome=";
	public static final String INIT_VALUE = PHP;

	@Override
	public void onValueChange(ValueChangeEvent<String> event) {
		String token = event.getValue();
		if (token.equals(PHP)) {
			UserBarPanel.getInstance().selectTab(ClientConstants.INDEX_PHP);
			MsgBoardPanel.getInstance().init();
			FriendsPreviewPanel.getInstance().init();
			UserInfoPanel.getInstance().init();
			UserStateListPanel.getInstance().init();
			RecentRecommendPanel.getInstance().init();
			MsgBoardPanel.getInstance().init();
			PHPListener.getInstance().getUserStateList(-1);
			PHPListener.getInstance().getMessageList(-1);
			PHPListener.getInstance().getUserRecentRecommend(-1);
			PHPListener.getInstance().getLatestState(-1);
			FriendListener.getInstance().getFriendList(-1);
		} else if (token.startsWith(PREFIX_USERHOME)) {
			try {
				int id = Integer.parseInt(token.substring(PREFIX_USERHOME
						.length()));
				UserBarPanel.getInstance().selectTab(ClientConstants.INDEX_PHP);
				FriendsPreviewPanel.getInstance().init();
				FriendListener.getInstance().getFriendList(id);
				UserInfoPanel.getInstance().init();
				PHPListener.getInstance().getLatestState(id);
				UserStateListPanel.getInstance().init();
				PHPListener.getInstance().getUserStateList(id);
				RecentRecommendPanel.getInstance().init();
				PHPListener.getInstance().getUserRecentRecommend(id);
				MsgBoardPanel.getInstance().init();
				PHPListener.getInstance().getMessageList(id);
			} catch (Exception e) {
				Window.Location.replace(Host.getPAGE_NOT_FOUND());
			}
		}
	}
}
